package cn.ixinjiu.libraryMangement.dao.impl;

import cn.ixinjiu.libraryMangement.dao.IUserInfoDao;
import cn.ixinjiu.libraryMangement.entity.UserInfo;
import cn.ixinjiu.libraryMangement.utils.JDBCUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by XinChen on 2023-05-08
 *
 * @TODO 个人中心（CRUD）的DAO层
 */
public class UserInfoDaoImpl implements IUserInfoDao {
    @Override
    public UserInfo login(String email, String password, int loginType) throws SQLException {
        ResultSet query = JDBCUtil.query("select * from user_info where email = ? and password = ? and role = ?", new Object[]{email, password, loginType});
        if (query != null) {
            while (query.next()) {
                int id = query.getInt("id");
                String userName = query.getString("user_name");
                String selectPassword = query.getString("password");
                String avatar = query.getString("avatar");
                String selectEmail = query.getString("email");
                String phoneNumber = query.getString("phone_number");
                int role = query.getInt("role");
                UserInfo userInfo = new UserInfo(id, userName, selectPassword, avatar, selectEmail, phoneNumber, role);
                if (email.equals(selectEmail) && password.equals(selectPassword)) {
                    return userInfo;
                }
            }
        }
        return null;
    }

    @Override
    public int register(UserInfo user) {
        int update = JDBCUtil.update("insert into user_info values (?, ?, ?, ?, ?, ?)",
                new Object[]{user.getId(), user.getUserName(), user.getPassword(), user.getEmail(), user.getPhoneNumber(), user.getRole()});
        return update;
    }

    @Override
    public List<UserInfo> userList() {
        try {
            ResultSet query = JDBCUtil.query("select * from user_info", null);
            List<UserInfo> users = new ArrayList<>();
            while (query.next()) {
                int id = query.getInt("id");
                String userName = query.getString("user_name");
                String password = query.getString("password");
                String avatar = query.getString("avatar");
                String email = query.getString("email");
                String phoneNumber = query.getString("phone_number");
                int role = query.getInt("role");
                UserInfo userInfo = new UserInfo(id, userName, password, avatar, email, phoneNumber, role);
                users.add(userInfo);
            }
            return users;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close();
        }
    }

    @Override
    public int count() {
        try {
            ResultSet query = JDBCUtil.query("SELECT COUNT(*) as count FROM `user_info`", null);
            int count = 0;
            while (query.next()) {
                count = query.getInt("count");
            }
            return count;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close();
        }
    }

    @Override
    public int userInfoDel(int id) {
        return JDBCUtil.update("delete from user_info where id = ?", new Object[]{id});
    }

    @Override
    public int userInfoUpd(UserInfo user) {
        String sql = "UPDATE `user_info` SET `user_name` = ?, `password` = ?, `avatar` = ? , `email` = ?, " +
                "`phone_number` = ?, `role` = ? WHERE `id`= ?;";
        Object[] objects = {user.getUserName(), user.getPassword(), user.getAvatar(), user.getEmail(),
                user.getPhoneNumber(), user.getRole(), user.getId()};
        return JDBCUtil.update(sql, objects);
    }

    @Override
    public UserInfo getUserInfoById(int bid) throws SQLException {
        ResultSet query = JDBCUtil.query("select * from user_info where id = ?", new Object[]{bid});
        while (query.next()) {
            int id = query.getInt("id");
            String userName = query.getString("user_name");
            String password = query.getString("password");
            String avatar = query.getString("avatar");
            String email = query.getString("email");
            String phoneNumber = query.getString("phone_number");
            int role = query.getInt("role");
            return new UserInfo(id, userName, password, avatar, email, phoneNumber, role);
        }
        return null;
    }
}
